<template>
     <importExcel :map="map" :xlsxMap="xlsxMap"  ref="importStu"
        @submit="submitExcel"
        ></importExcel>
</template>
<script>
import importExcel from './importExcel'
import XlsxMap from './xlsxMap'
import {importDept} from '@/api/deptApi'
export default {
    components: {
        importExcel
    },
    props: {
        schoolId: String
    },
    data(){
        return {
            map: {
                部门名称: {col: 'name', required: true},
                部门编码: {col: 'code', reg: /^\d+$/, desc: '只允许填写数字'},
                上级部门: {col: 'parentName'}
            },
            xlsxMap: new XlsxMap()
        }
    },
    methods: {
        async importDeptData({list,schoolId}){
            list = this.xlsxMap.formatData(list)
    
            var params = {
                schoolId: schoolId,
                body: list
            }
            
            await importDept(params)
        },
        async submitExcel(data){
            try {
                while (true) {
                    var temp = data.splice(0, 500)
                    if (temp.length > 0) {
                         await this.importDeptData({list: temp, schoolId: this.schoolId})
                          this.$refs.importStu.progress ++
                    } else {
                        break
                    }
                }

                this.$message.success('导入部门数据完成')
                this.$refs.importStu.complete()
             
                this.$store.dispatch('initSchool/putSchool', {
                    schoolId: this.schoolId,
                    initState: 1
                })

                 
            } catch (error) {
                this.errList.push('部门数据导入失败')
            }
        }
    }
}
</script>
<style scoped>

</style>